package  com.py.net.proto;
import java.util.HashMap;
import java.util.Map;

import com.google.protobuf.MessageLite;
import com.google.protobuf.MessageLite.Builder;
import com.py.net.client.businese.ClientChatHandle;
import com.py.net.client.businese.ClientChatRetHandle;
import com.py.net.client.businese.ClientLoginHandle;
import com.py.net.server.businese.ServerChatHandle;
import com.py.net.server.businese.ServerLoginHandle;

public class ProtoMapper {
	
	private static Map<Integer, ProtoMapperEnum> typeMessageMap = new HashMap<>();
	
	private static Map<Class<? extends MessageLite>, ProtoMapperEnum> messageTypeMap = new HashMap<>();
	
	public static void init() {
		for (ProtoMapperEnum e : ProtoMapperEnum.values()) {
			if (typeMessageMap.containsKey(e.type)) {
				throw new RuntimeException("重复proto协议类型");
			}
			if (messageTypeMap.containsKey(e.clazz)) {
				throw new RuntimeException("重复proto协议class");
			}
			typeMessageMap.put(e.type, e);
			messageTypeMap.put(e.clazz, e);
		}
	}
	
	public static Class<? extends MessageLite> getMessageClass(int type) {
		return typeMessageMap.get(type).clazz;
	}
	
	public static int getMessageType(Class<? extends MessageLite> clazz) {
		return messageTypeMap.get(clazz).type;
	}
	
	public static ProtoMapperEnum getProtoMapperEnum(Class<? extends MessageLite> clazz) {
		return messageTypeMap.get(clazz);
	}
	
	public enum ProtoMapperEnum {
		CTSChat(1, ChatProbuf.CTSChat.class, new ServerChatHandle()),//客户端给服务端发送聊天协议
		STCChat(2, ChatProbuf.STCChat.class, new ClientChatHandle()),//服务端给客户端推送聊天协议
		STCChatRet(3, ChatProbuf.STCChatRet.class, new ClientChatRetHandle()),//服务端给客户端推送聊天协议
		CTSLogin(4, ChatProbuf.CTSLogin.class, new ServerLoginHandle()),//服务端给客户端推送登陆协议
		STCLogin(5, ChatProbuf.STCLogin.class, new ClientLoginHandle());//服务端给客户端推送聊天协议
		
		/** 协议号 */
		public int type;
		/** 请求的proto类 */
		public Class<? extends MessageLite> clazz;
		/** 业务处理逻辑类 */
		public IBusineseHandle<?> handle;
		
		public Class<Builder> builderClass;
		
		private ProtoMapperEnum(int type, Class<? extends MessageLite> clazz, IBusineseHandle<?> handle) {
			this.type = type;
			this.clazz = clazz;
			this.handle = handle;
		}
	}
}
